// 引入axios
import axios from 'axios';
/*
AxiosResponse：定义 Axios 响应对象的结构
AxiosInstance：定义 Axios 实例的类型
AxiosError：定义 Axios 错误对象的类型
InternalAxiosRequestConfig：定义 Axios 请求配置对象的内部类型
* */
import type { AxiosResponse, AxiosInstance, AxiosError, InternalAxiosRequestConfig } from 'axios';

// 定义基础URL
let baseUrl = "http://localhost:8000/";

// 创建axios实例
const httpService: AxiosInstance = axios.create({
    // 设置基础URL
    baseURL: baseUrl,
    // 设置请求超时时间
    timeout: 3000
});

// 添加请求和响应拦截器
// 在请求发送前进行处理
httpService.interceptors.request.use(function (config: InternalAxiosRequestConfig) {
    // 添加授权令牌
    if (config.headers) {
        config.headers.AUTHORIZATION = window.sessionStorage.getItem('token') || '';
    }
    return config;
}, function (error: AxiosError) {
    // 处理请求错误
    return Promise.reject(error);
});

// 处理响应数据
httpService.interceptors.response.use(function (response: AxiosResponse) {
    return response;
}, function (error: AxiosError) {
    // 处理响应错误
    return Promise.reject(error);
});

/* 网络请求部分 */

/**
 * 发起GET请求
 * @param {string} url - 请求地址
 * @param {object} params - 请求参数
 * @returns {Promise} - 返回Promise对象
 */
export function get<T = any>(url: string, params: object = {}): Promise<AxiosResponse<T>> {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            params: params
        }).then((response: AxiosResponse<T>) => {
            resolve(response);
        }).catch((error: AxiosError) => {
            reject(error);
        });
    });
}

/**
 * 发起POST请求
 * @param {string} url - 请求地址
 * @param {object} params - 请求参数
 * @returns {Promise} - 返回Promise对象
 */
export function post<T = any>(url: string, params: object = {}): Promise<AxiosResponse<T>> {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params
        }).then((response: AxiosResponse<T>) => {
            console.log(response)
            resolve(response);
        }).catch((error: AxiosError) => {
            console.log(error)
            reject(error);
        });
    });
}

/**
 * 发起DELETE请求
 * @param {string} url - 请求地址
 * @param {object} params - 请求参数
 * @returns {Promise} - 返回Promise对象
 */
export function del<T = any>(url: string, params: object = {}): Promise<AxiosResponse<T>> {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'delete',
            data: params
        }).then((response: AxiosResponse<T>) => {
            console.log(response)
            resolve(response);
        }).catch((error: AxiosError) => {
            console.log(error)
            reject(error);
        });
    });
}

/**
 * 文件上传
 * @param {string} url - 请求地址
 * @param {object} params - 请求参数
 * @returns {Promise} - 返回Promise对象
 */
export function fileUpload<T = any>(url: string, params: object = {}): Promise<AxiosResponse<T>> {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: {'Content-Type': 'multipart/form-data'}
        }).then((response: AxiosResponse<T>) => {
            resolve(response);
        }).catch((error: AxiosError) => {
            reject(error);
        });
    });
}

// 获取服务器URL
export function getServerUrl(): string {
    return baseUrl;
}

// 定义导出对象的类型
interface RequestUtil {
    get: <T = any>(url: string, params?: object) => Promise<AxiosResponse<T>>;
    post: <T = any>(url: string, params?: object) => Promise<AxiosResponse<T>>;
    del: <T = any>(url: string, params?: object) => Promise<AxiosResponse<T>>;
    fileUpload: <T = any>(url: string, params?: object) => Promise<AxiosResponse<T>>;
    getServerUrl: () => string;
}

// 导出请求方法和获取服务器URL的函数
const requestUtil: RequestUtil = {
    get,
    post,
    del,
    fileUpload,
    getServerUrl
};

export default requestUtil;
